package cn.jaye.fabric.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import cn.jaye.fabric.application.CommodityApplicationService;
import cn.jaye.fabric.application.command.CreateCommodityCommand;
import cn.jaye.fabric.application.command.InfoCommodityCommand;
import cn.jaye.fabric.application.command.ListCommodityCommand;
import cn.jaye.fabric.common.constant.Global;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jaye
 * @create 2018-02-08 16:04
 */
@RestController
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private CommodityApplicationService commodityApplicationService;

    @PostMapping("/list")
    public String listsApiPackage(HttpServletRequest request) {
        JSONObject requstParam = JSON.parseObject(request.getParameter("aodata"));
        int sEcho = requstParam.getIntValue("sEcho");
        String sSearch = requstParam.getString("sSearch");//查询条件
        Integer start = requstParam.getInteger("iDisplayStart");
        Integer length = requstParam.getInteger("iDisplayLength");
        Integer currentPage;
        currentPage = (start / length) + 1;
        Map<String, Object> map = new HashMap<>();
        map.put("aaData", new ArrayList<>());
        map.put("iTotalDisplayRecords", 0);
        map.put("iTotalRecords", 0);
        map.put("sEcho", sEcho + 1);
        ListCommodityCommand listCommodityCommand = new ListCommodityCommand();
        listCommodityCommand.setSizes(length);
        listCommodityCommand.setCurrentPage(currentPage);
        if (StringUtils.isNotBlank(sSearch)) {
            listCommodityCommand.setCondition(sSearch);
        }
        String data = commodityApplicationService.listApiPackage(listCommodityCommand);
        JSONObject jsonObject = JSON.parseObject(data);
        if (Global.SUCCESS_FLAG.equals(jsonObject.getInteger("code"))) {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("records");
            if (jsonArray.size() > 0) {
                List<Map> r = jsonArray.toJavaList(Map.class);
                map.put("aaData", r);
                map.put("iTotalDisplayRecords", jsonObject.getJSONObject("data").getInteger("total"));
                map.put("iTotalRecords", jsonObject.getJSONObject("data").getInteger("total"));
                map.put("sEcho", sEcho + 1);
            }
        }
        return JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
    }

    @PostMapping("/create")
    public String createCommodity(HttpServletRequest request, @RequestBody String data) {
        CreateCommodityCommand createCommodityCommand = JSON.parseObject(data, CreateCommodityCommand.class);
        return commodityApplicationService.createCommodity(createCommodityCommand);
    }
    @PostMapping("/info")
    public String info(HttpServletRequest request, @RequestBody String data) {
        InfoCommodityCommand infoCommodityCommand = JSON.parseObject(data, InfoCommodityCommand.class);
        return commodityApplicationService.getCommodity(infoCommodityCommand);
    }

}
